package code.sbxj.qwd.com.sbxj;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.NetworkUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import code.sbxj.qwd.com.sbxj.login.suggest.LoginAct;


@ContentView(R.layout.activity_mylogin)
public class LoginActivity extends BaseActivity {
    @ViewInject(R.id.webview)
    private WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview.setWebViewClient(new MyWebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(Config.BaseUrl);

       if(!NetworkUtils.isAvailableByPing(Config.BaseIP)){

           AlertDialog.Builder normalDialog =
                   new AlertDialog.Builder(LoginActivity.this);
           normalDialog.setTitle("温馨提示");
           normalDialog.setMessage("当前网络不可用，是否启用离线模式？");
           normalDialog.setPositiveButton("确定",
                   new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {

                           CacheDiskUtils.getInstance().remove("cookie");
                           Intent intent =new Intent();
                           intent.setClass(LoginActivity.this, MainActivity.class);
                           startActivity(intent);
                       }
                   });
           normalDialog.setNegativeButton("退出",
                   new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                          finish();
                       }
                   });
           // 显示
           normalDialog.show();
       }
    }


    public class MyWebViewClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView webview, String url) {
            webview.loadUrl(url);
            Log.e("url",url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            CookieManager cookieManager = CookieManager.getInstance();
            String CookieStr = cookieManager.getCookie(url);
            String title = view.getTitle();
            if (!TextUtils.isEmpty(title)) {

                Log.e("title",title);

                if(title.contains("主页")){
                    CacheDiskUtils.getInstance().put("cookie",CookieStr);

                    if(getIntent().getExtras()!=null){
                        if(!getIntent().getExtras().getBoolean("istomain",true)){
                            finish();
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

                    Intent intent =new Intent();
                    intent.setClass(LoginActivity.this, MainActivity.class);
                    startActivity(intent);

                    finish();

                }


            }
            super.onPageFinished(view, url);
        }

    }



//    private void getUserInfo() {
//        ShowLoading();
//        RequestParams params = new RequestParams(Config.BaseUrl+"mgr/currentInfo");
//        params.setUseCookie(true);
//        params.addHeader("cookie","shiroCookie=22ddaf92-3b7c-472a-a626-9ab645e79ba6");
//        x.http().get(params, new Callback.CommonCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//
//                Log.e("result",result);
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
//            }
//
//            @Override
//            public void onFinished() {
//                CancleLoading();
//            }
//        });
//    }
}
